package com.cskaoyan.service;

import com.cskaoyan.bean.MarketLog;
import com.cskaoyan.bean.MarketLogExample;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketLogMapper;
import com.cskaoyan.utils.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: JianhuaZeng
 * @Date: 2022/12/30 20:02
 */
@Service
public class LogServiceImpl implements LogService{

    @Autowired
    MarketLogMapper logMapper;

    @Override
    public CommonData<MarketLog> list(BasePageInfo pageInfo) {
        PageHelper.startPage(pageInfo.getPage(),pageInfo.getLimit());
        MarketLogExample logExample = new MarketLogExample();
        MarketLogExample.Criteria criteria = logExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if(!StringUtil.isEmpty(pageInfo.getName())){
            criteria.andAdminLike("%" + pageInfo.getName() + "%");
        }
        logExample.setOrderByClause(pageInfo.getSort() + " " + pageInfo.getOrder());
        List<MarketLog> logs = logMapper.selectByExample(logExample);
        PageInfo<MarketLog> logPageInfo = new PageInfo<>(logs);

        // 构造返回对象
        CommonData<MarketLog> logData = new CommonData<>();
        logData.setPage(pageInfo.getPage());
        logData.setLimit(pageInfo.getLimit());
        logData.setPages(logPageInfo.getPageSize());
        logData.setTotal((int) logPageInfo.getTotal());
        logData.setList(logs);
        return logData;
    }

    /**
     * @description: 插入log
     * @param: marketLog
     * @return: void
     * @author: JianhuaZeng
     * @date: 2022/12/30 2022/12/30
     */
    public void insert(MarketLog marketLog){
        logMapper.insertSelective(marketLog);
    }
}
